package com.example.newdemo.view.recyclerView.grid;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.newdemo.R;
import com.example.newdemo.bean.IndexBean;

import java.util.List;

public class GridAdapter extends RecyclerView.Adapter<GridAdapter.GridViewHolder>{


    private Context context;
    private List<IndexBean> indexList;
    private OnItemStateIndexListener listener;

    public GridAdapter(Context context , List<IndexBean> indexList){
        this.context = context;
        this.indexList = indexList;
    }

    public void setOnItemStateListener(OnItemStateIndexListener listener) {
        this.listener = listener;
    }

    public void setIndexList(List<IndexBean> indexList){
        this.indexList = indexList;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public GridViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_view1, parent, false);

//        View view = new TextView(context);
        GridViewHolder holder = new GridViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull GridViewHolder holder, int position) {
        IndexBean bean = indexList.get(position);
        holder.title.setText(bean.name);
        holder.number.setText((position + 1)+"");
        holder.rlBody.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onItemClick(view,position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return indexList.size();
    }


    public  class GridViewHolder extends RecyclerView.ViewHolder{

        public TextView title;
        public TextView number;
        public RelativeLayout rlBody;
        public GridViewHolder(View v) {
            super(v);

            ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(350,350);
            title = (TextView) v.findViewById(R.id.title);
            number = (TextView) v.findViewById(R.id.number);


            rlBody = v.findViewById(R.id.ll_item_body);
            rlBody.setLayoutParams(layoutParams);
        }
    }


    public interface OnItemStateIndexListener {
        void onItemClick(View view, int position);
    }


    @Override
    public void onViewRecycled(@NonNull GridViewHolder holder) {
        //
        super.onViewRecycled(holder);
    }



}
